package cn.xiaobai;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @Author yangdaji
 * @Date 2024/2/17
 * @PackageName cn.xiaobai
 * @ClassName SqliteApplication
 */
@SpringBootApplication
public class SqliteApplication implements CommandLineRunner {

//    @Autowired
//    private DataSource dataSource;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private UserService userService;

    public static void main(String[] args) {
        SpringApplication.run(SqliteApplication.class, args);
    }


    @Override
    public void run(String... args) throws Exception {
//        userService.queryUser();

        // 创建表
        jdbcTemplate.execute("create table if not exists user(\n" +
                "  id integer primary key autoincrement,\n" +
                "  name varchar\n" +
                ");\n" +
                "\n");

        // 插入数据
        jdbcTemplate.execute("insert into user(name)\n" +
                "values ('小米'),\n" +
                "       ('华为');");


        // 查询数据
        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from user;");
        for (Map<String, Object> map : maps) {
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                System.out.print(entry.getKey() + "=" + entry.getValue() + "\t");
            }
            System.out.println();
        }


    }
}
